package myJava.Web;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WEBUtil {
	//获取网页数据
	public static String getWebTxt(String url) {
        URL u;
        HttpURLConnection httpURLConnection;
        String ret = "";
        try {
        	
            u = new URL(url);
            httpURLConnection = (HttpURLConnection)u.openConnection();
            
            if (httpURLConnection.getResponseCode() == 200) {
            	
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8"));

                String read;
                while ((read = bufferedReader.readLine()) != null) {
                    ret += read ;
                    ret += "\r\n" ;
                }
            }
        } catch (Exception e){
        	System.out.println("解析网页错误!");
        }
        return ret ;
    }
	//正则匹配
	public static String takeWebDetail(String str, String pattern) {
		
		Pattern r = Pattern.compile(pattern);
	    Matcher m = r.matcher(str);
	    if (m.find( )) {
	    	//System.out.println("Found value: " + m.group(0));
	    	return m.group(0);
	    } else {
	    	System.out.println("NO MATCH");
	    }
		
		return "";
	}
	//unicode字符串转unicode字符
	public static String stringToChar(String unicode) {
		//System.out.println(unicode);
	    String string = "";
	    String temp = "";
	    int len = unicode.length();
	    for (int i = 0; i < len; ) {
	    	if (unicode.charAt(i) == '\\' && unicode.charAt(i + 1) == 'u' && i + 1 < len) {
	    		//i+2--i+5
	    		temp = unicode.substring(i + 2, i + 6);
	    		i += 6;
	    		string += string4ToInt(temp);
	    	}else {
	    		//System.out.println(unicode.charAt(i));
	    		while (i < len) {
	    			if (i + 1 >= len) {
	    				string += unicode.charAt(i);
	    				i++;
	    				break;
	    			}else if (unicode.charAt(i + 1) == 'u' && unicode.charAt(i) == '\\') {
	    				break;
	    			}
	    			string += unicode.charAt(i);
	    			i++;
    			}
	    	}
	    }
	    return string;
	}
	//unicode转char
	public static char string4ToInt(String str) {
		int data = Integer.parseInt(str, 16);//16进制转10进制
		return ((char)data);
	}
	
	//删除指定字符
	public static String deleteChar(String str, char delChar) {
        String delAfterStr = "";
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) != delChar) {
            	delAfterStr += str.charAt(i);
            }
        }
        return delAfterStr;
    }
	
	//下载image
	public static int downloadImage(String imageUrl, String downfileName) {
		//System.out.println(imageUrl);
		String newImageName = "src/myJava/Web/images/" + downfileName;
		File imageFileOutp = new File(newImageName);
		if (imageFileOutp.exists()) {
			System.out.println("File is exist so donot download");
			return -1;
		}
		try {
			URL url = new URL(imageUrl);
			
			//打开网络输入流
			DataInputStream dis = new DataInputStream(url.openStream());
			
			
			//建立一个新的文件
			FileOutputStream fos = new FileOutputStream(imageFileOutp);
			
			byte[] buffer = new byte[1024];
			int length;
			//开始填充数据
			while ((length = dis.read(buffer)) > 0) {
				fos.write(buffer, 0, length);
			}
			dis.close();
			fos.close();
			return 0;
		} catch (IOException e) {
			System.out.println("image download failed");
			return -1;
		}
	}
	//获取当前时间年月日
	public static Time getNowTime() {
		String time = "";     
	    Date dt = new Date();
	    
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");     
	    time = sdf.format(dt);
	    
	    String Nowtime[] = time.split("-");
		int year = Integer.parseInt(Nowtime[0]);
		int month = Integer.parseInt(Nowtime[1]);
		int day = Integer.parseInt(Nowtime[2]);
	    
        return new Time(year, month, day);
	}
	//返回每月的最后一天
	public static int getMonthend(int year, int month) {
		int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 ,31};
		//判断是否为闰年
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
			days[2] += 1;
		}
		return days[month];
	}
	public static String changeChar(String str, char ch) {
		String afterStr = "";
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) != ch) {
            	afterStr += str.charAt(i);
            }else {
            	afterStr += '_';
            }
        }
        return afterStr;
	}
}
